
#!/bin/bash

# Set parameters for bond0 interface

BOND0_IP=192.168.31.100
BOND0_NETMASK=24
BOND0_GATEWAY=192.168.31.1

# delete bond0 interface and slave interfaces
nmcli con delete bond0

# clean up old bond0 interface and slave interfaces
rm -f /etc/sysconfig/network-scripts/ifcfg-bond0*
rm -f /etc/sysconfig/network-scripts/ifcfg-bond-slave-eth0*
rm -f /etc/sysconfig/network-scripts/ifcfg-bond-slave-eth1*


# restart NetworkManager to clear internal state 
systemctl restart NetworkManager

sleep 5

# Create bond0 interface
# nmcli con add type bond ifname bond0 con-name bond0 mode active-backup 
nmcli con add type bond ifname bond0 con-name bond0 mode active-backup

# Add slave interfaces to bond0 interface
nmcli con add type bond-slave ifname eth0 master bond0
nmcli con add type bond-slave ifname eth1 master bond0

# Set IP address for bond0 interface
nmcli con modify bond0 ipv4.addresses $BOND0_IP/$BOND0_NETMASK
nmcli con modify bond0 ipv4.gateway $BOND0_GATEWAY
nmcli con modify bond0 ipv4.dns 8.8.8.8
nmcli con modify bond0 ipv4.method manual

# Set bond options for bond0 interface (mode=active-backup, primary=eth0)
# nmcli con modify bond0 bond.options "mode=active-backup;primary=eth0"

# Remove IP address from slave interfaces (eth0 and eth1)
nmcli con modify bond-slave-eth0 ipv4.method disabled
nmcli con modify bond-slave-eth1 ipv4.method disabled

# Enable bond0 interface and slave interfaces
nmcli con up bond0
nmcli con up bond-slave-eth0
nmcli con up bond-slave-eth1

# Show bond0 interface and slave interfaces
# nmcli con show bond0
# nmcli con show bond-slave-eth0
# nmcli con show bond-slave-eth1
nmcli dev status